#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void reverse_word(char* word, int nlen=-1){
     int nlast = strlen(word)-1;
     int nbegin = 0;
     if(nlen !=-1)
         nlast = nlen-1;
         
     while(nbegin < nlast){
         char ch=word[nlast];
         word[nlast]=word[nbegin];
         word[nbegin] = ch;
         nbegin ++;
         nlast --;
     }
}


void reverse_string(char* str){
    reverse_word(str);
    int nbegin = 0;
    int nend = 0;
    while(1){
        if( str[nend] == ' ' || !str[nend] ){
            reverse_word(str+nbegin, nend-nbegin);
            if(!str[nend])
                break;
            nbegin = nend+1;
            nend ++;            
        }else{
            nend ++;
        }
    }

}
int main(int argc, char *argv[])
{
    char word[]="abcde";
    reverse_word(word);
    cout<<word<<endl;

    char str[]="hello world this is me";
    reverse_string(str);
    cout<<str<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
